//
//  HMComposeToolBar.swift
//  Weibo
//
//  Created by Apple on 2017/7/16.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

enum HMComposeToolBarButtonType: Int {
    case picture = 0
    case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
}

//  自定义toolbar
class HMComposeToolBar: UIStackView {

    var callBack: ((HMComposeToolBarButtonType)->())?
    //  表情按钮
    var emoticonButton: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //  添加控件
    private func setupUI() {
        self.axis = .horizontal
        self.distribution = .fillEqually
        addChildButton(imageName: "compose_toolbar_picture", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  添加子按钮
    @discardableResult
    private func addChildButton(imageName: String, type: HMComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
        return button
    }
    
    //  MARK:   --点击按钮事件处理
    @objc private func buttonAction(btn: UIButton) {
        let type = HMComposeToolBarButtonType(rawValue: btn.tag)!
        callBack?(type)
    }
    
    //  根据是否是表情键盘切换对应的icon
    func switchButtonIcon(isEmoticon: Bool) {
        if isEmoticon {
            //  表情类型显示系统的icon
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        } else {
            //  系统类型显示表情icon
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
    
}
